package com.rivulet.doj_backend.service.impl;

import com.rivulet.doj_backend.mapper.ClassMapper;
import com.rivulet.doj_backend.pojo.Class;
import com.rivulet.doj_backend.service.ClassService;
import com.rivulet.doj_backend.tools.ResultFul;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ClassServiceImpl implements ClassService {
    @Autowired
    ClassMapper classMapper;

    @Override
    public ResultFul setUserJoinClass(Integer user_id,Integer class_id) {
        ResultFul result;
        try{
            if(user_id != null && class_id != null) {
                classMapper.setUserJoinClass(user_id, class_id);
                result = new ResultFul<Object>(100, null);
            }else {
                result = new ResultFul<Object>(408, null);
            }
        }catch (Exception e) {
            System.out.println("setUserJoinClass_error:" + e);
            result = new ResultFul<Object>(404, null);
        }

        return result;
    }


    @Override
    public ResultFul inquiryClass(Integer user_id){
        ResultFul result;
        try{
            if (user_id != null){
                Integer realclassId = classMapper.getClassIdByUserId(user_id);
                List list = new ArrayList();
                list.add(realclassId);
                result = new ResultFul<List>(100, list);
            }else {
                result = new ResultFul<Object>(408, null);
            }

        }catch (Exception e){
            System.out.println("showQuestion_error:"+e);
            result = new ResultFul<Object>(404, null);
        }
        return result;
    }

    @Override
    public ResultFul showClass(Integer user_id,Integer class_id) {
        ResultFul result;
        try{
            if(user_id != null && class_id != null) {
                Class realclass= classMapper.showClass(class_id);
                List list = new ArrayList();
                list.add(realclass);
                result = new ResultFul<List>(100, list);
            }else {
                result = new ResultFul<Object>(408, null);
            }

        }catch (Exception e) {
            System.out.println("showClass_error:" + e);
            result = new ResultFul<Object>(404, null);
        }

        return result;
    }

    @Override
    public ResultFul delClass(Integer user_id,Integer class_id) {
        ResultFul result;
        try{
            if(user_id != null && class_id != null) {
                classMapper.delClass(class_id);
                result = new ResultFul<Object>(100, null);
            }else {
                result = new ResultFul<Object>(408, null);
            }

        }catch (Exception e) {
            System.out.println("delClass_error:" + e);
            result = new ResultFul<Object>(404, null);
        }

        return result;
    }

    @Override
    public ResultFul showTask(Integer user_id){
        ResultFul result;
        try{
            if(user_id != null) {
                Integer realClassTaskId = classMapper.showTask(user_id);
                List list = new ArrayList();
                list.add(realClassTaskId);
                result = new ResultFul<List>(100, list);
            }else {
                result = new ResultFul<Object>(408, null);
            }

        }catch (Exception e) {
            System.out.println("showTask_error:" + e);
            result = new ResultFul<Object>(404, null);
        }

        return result;
    }

    @Override
    public ResultFul delUser(Integer user_id,Integer class_id){
        ResultFul result;
        try{
            if(user_id != null && class_id != null) {
                classMapper.delUser(user_id, class_id);
                result = new ResultFul<Object>(100, null);
            }else {
                result = new ResultFul<Object>(408, null);
            }

        }catch (Exception e) {
            System.out.println("delUser_error:" + e);
            result = new ResultFul<Object>(404, null);
        }

        return result;
    }

    @Override
    public ResultFul addTask(Integer user_id, Integer task_id, Integer question_number, int question_ids, Integer question_score, String task_explain, Integer relation_class_id) {
        ResultFul result;
        try {
            this.classMapper.addTask(task_id, question_number, question_ids, question_score, task_explain, relation_class_id);
            this.classMapper.addTask2(user_id, task_id);
            result = new ResultFul(Integer.valueOf(100), (Object)null);
        } catch (Exception var10) {
            System.out.println("addTask_error:" + var10);
            result = new ResultFul(Integer.valueOf(404), (Object)null);
        }

        return result;
    }

    @Override
    public ResultFul addClass(Integer user_id, Integer class_id, String class_root, Integer task_number, Integer user_number, Integer creat_time, Integer class_level) {
        ResultFul result;
        try {
            this.classMapper.addClass(user_id, class_id, class_root, task_number, user_number, creat_time, class_level);
            this.classMapper.addClass2(user_id, class_id);
            result = new ResultFul(Integer.valueOf(100), (Object)null);
        } catch (Exception var10) {
            System.out.println("addTask_error:" + var10);
            result = new ResultFul(Integer.valueOf(404), (Object)null);
        }

        return result;
    }

    @Override
    public ResultFul showPeopleNumber(Integer class_id) {
        ResultFul result;
        try {
            int e = this.classMapper.getPeopleNumber(class_id).intValue();
            result = new ResultFul(Integer.valueOf(100), Integer.valueOf(e));
        } catch (Exception var4) {
            System.out.println("addTask_error:" + var4);
            result = new ResultFul(Integer.valueOf(404), (Object)null);
        }

        return result;
    }


}
